from django.http import HttpResponseRedirect
from django.shortcuts import render, redirect

# Create your views here.
from django.urls import reverse


from .models import TextModel, CommentModel


def index(request):
    data = TextModel.objects.all()
    return render(request, 'lavoz/index.html',context={"xxx": data,"notice":"Welcome My Blog!"})


def comment(request, tid):
    if request.method == "POST":
        pubtime = request.POST.get("time")
        comment_comment = request.POST.get("content")

        com_obj = CommentModel(comment_content=comment_comment,comment_time=pubtime,text_id_id=tid)
        com_obj.save()
        return redirect(reverse('lavoz:detail',kwargs={'tid': tid}))
    data = TextModel.objects.get(id=tid)

    return render(request, 'lavoz/comment.html', context={"xx": data})


def detail(request, tid):
    data = TextModel.objects.get(id=tid)
    comment_data = CommentModel.objects.filter(text_id_id=tid)

    return render(request, 'lavoz/details.html',context = {"xx": data, "comment_data":comment_data})


def message(request):
    return render(request, 'lavoz/message.html')


def about(request):
    return render(request, 'lavoz/about.html')